package com.pycredit.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.pycredit.exception.BizException;
import com.pycredit.type.ExceptionType;

/**
 * @category 参数格式化
 * 
 * @author yiz
 * @date 2016年9月6日 下午9:48:22
 * @version 1.0.0 
 * @copyright pycredit.cn 
 */
public class ParamFormatUtil {

	/**
	 * @category 用户风险参数格式化
	 * @param userRiskParamArr
	 * @return
	 * @throws BizException
	 */
	public static List<List<String[]>> getUserRiskParamFormat(String[] userRiskParams) throws BizException {
		List<List<String[]>> list = new ArrayList<List<String[]>>();
		for (String userRiskParam : userRiskParams) {
			if (StringUtils.isBlank(userRiskParam)) {
				throw new BizException(ExceptionType.CA_0003.getCode(), ExceptionType.CA_0003.getMsg());
			} else {
				try {
					String[] vals = userRiskParam.split(";");
					//array转List
					List<String[]> curList = new ArrayList<String[]>();
					for (String val : vals) {
						//切割,
						String[] ss = val.split(",");
						curList.add(ss);
					}
					list.add(curList);
				} catch (Exception e) {
					throw new BizException(ExceptionType.CA_0002.getCode(), ExceptionType.CA_0002.getMsg());
				}
			}
		}
		return list;
	}

	/**
	 * @category 日常分摊比例格式化
	 * @param dailyFeeRatio
	 * @return
	 * @throws BizException
	 */
	public static List<Double> getDailyFeeRatioFormat(Double[] dailyFeeRatio) throws BizException {
		List<Double> list = new ArrayList<Double>();
		if (null == dailyFeeRatio || dailyFeeRatio.length != 12) {
			throw new BizException(ExceptionType.CA_0003.getCode(),
					ExceptionType.CA_0003.getMsg() + ",参数dailyFeeRatio");
		} else {
			try {
				for (Double val : dailyFeeRatio) {
					list.add(val);
				}
			} catch (Exception e) {
				throw new BizException(ExceptionType.CA_0002.getCode(), ExceptionType.CA_0002.getMsg());
			}
		}
		return list;
	}

	/**
	 * @category 得到ABC三类人员薪水
	 * @param personSalarys
	 * @return
	 * @throws BizException 
	 */
	public static Map<String, Double> getPersonSalaryFormat(Double[] personSalarys) throws BizException {
		Map<String, Double> personSalaryMap = new HashMap<String, Double>();
		if (null == personSalarys || personSalarys.length != 3) {
			throw new BizException(ExceptionType.CA_0003.getCode(),
					ExceptionType.CA_0003.getMsg() + ",参数personSalarys");
		} else {
			//加入至Map中
			personSalaryMap.put("person_A", personSalarys[0]);
			personSalaryMap.put("person_B", personSalarys[1]);
			personSalaryMap.put("person_C", personSalarys[2]);
		}
		return personSalaryMap;
	}

	/**
	 * @category 得到字段成本系数,field_type_A,field_type_B,field_type_C 分别表示A，B，C字段字数
	 * @param filedCostRatios
	 * @return
	 * @throws BizException
	 */
	public static Map<String, Double> getFiledTypeFormat(Double[] filedCostRatios) throws BizException {
		Map<String, Double> filedCostRatioMap = new HashMap<String, Double>();
		if (null == filedCostRatios || filedCostRatios.length != 3) {
			throw new BizException(ExceptionType.CA_0003.getCode(),
					ExceptionType.CA_0003.getMsg() + ",参数filedCostRatios");
		} else {
			filedCostRatioMap.put("field_type_A", filedCostRatios[0]);
			filedCostRatioMap.put("field_type_B", filedCostRatios[1]);
			filedCostRatioMap.put("field_type_C", filedCostRatios[2]);
		}
		return filedCostRatioMap;
	}

	/**
	 * @category 产品基础成本格式化
	 * @param productBaseCosts
	 * @return
	 * @throws BizException
	 */
	public static Map<String, Double> getProductBaseCostFormat(Double[] productBaseCosts) throws BizException {
		Map<String, Double> productBaseCostMap = new HashMap<String, Double>();
		if (null == productBaseCosts || productBaseCosts.length != 11) {
			throw new BizException(ExceptionType.CA_0003.getCode(),
					ExceptionType.CA_0003.getMsg() + ",参数productBaseCosts");
		} else {
			productBaseCostMap.put("JKHC_XN", productBaseCosts[0]);
			productBaseCostMap.put("JKHC_ZX", productBaseCosts[1]);
			productBaseCostMap.put("JKHC_DB", productBaseCosts[2]);
			productBaseCostMap.put("GXPT_1", productBaseCosts[3]);
			productBaseCostMap.put("GXPT_2", productBaseCosts[4]);
			productBaseCostMap.put("GXPT_3", productBaseCosts[5]);
			productBaseCostMap.put("GXPT_4", productBaseCosts[6]);
			productBaseCostMap.put("GXPT_5", productBaseCosts[7]);
			productBaseCostMap.put("ZZHC_C", productBaseCosts[8]);
			productBaseCostMap.put("PLHC_C", productBaseCosts[9]);
			productBaseCostMap.put("PLDB_C", productBaseCosts[10]);
		}
		return productBaseCostMap;
	}

	/**
	 * @category 得到value值
	 * @param value
	 * @return
	 * @throws BizException
	 */
	public static Map<String, String[]> getValuesFormat(String[][] valueX) throws BizException {
		if (null == valueX || valueX.length != 8) {
			throw new BizException(ExceptionType.CA_0003.getCode(), ExceptionType.CA_0003.getMsg() + ",参数valueX");
		}
		Map<String, String[]> values = new HashMap<String, String[]>();

		for (int i = 0; i < valueX.length; i++) {
			values.put("C" + (i + 1), valueX[i][0].split(";"));
		}
		return values;
	}

	/**
	 * @category 得到valueX值
	 * @param valueX
	 * @return
	 * @throws BizException
	 */
	public static Map<String, String[]> getValuesXFormat(String[][] valueX) throws BizException {
		if (null == valueX || valueX.length != 8) {
			throw new BizException(ExceptionType.CA_0003.getCode(), ExceptionType.CA_0003.getMsg() + ",参数valueX");
		}
		Map<String, String[]> valuesX = new HashMap<String, String[]>();

		for (int i = 0; i < valueX.length; i++) {
			valuesX.put("C" + (i + 1), valueX[i][1].split(";"));
		}
		return valuesX;
	}
}
